"use strict"

import React from 'react';
import {BASE_URL} from '../const/const';
import user from '../common/user'
import Storage from '../util/storage_util';

// 网络请求封装
class NetUtil extends React.Component {

    //post请求
    /**
    *url :请求地址
    *data:参数
    *callback:回调函数
    */
    static postFrom(url, data, callback) {
        let paramsArray = [];
        Object.keys(data).forEach(key => paramsArray.push(key + '=' + data[key]))
        let token=user.user.token?user.user.token:''
        var fetchOptions = {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization':token
            },
            body: paramsArray.join('&')
        };
        fetch(BASE_URL+url, fetchOptions)
          .then((response) => response.json())
          .then((json) => {
              callback(json);
          }).done();
    }

    /**
     *url :请求地址
     *data:参数(Json对象)
     *callback:回调函数
     */
    static postJson(url, data, callback) {
        let token=user.user.token?user.user.token:''
        var fetchOptions = {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                //json形式
                'Content-Type': 'application/json',
                'Authorization': token
            },
            body: JSON.stringify(data)
        };
        console.log('postJson------' + JSON.stringify(fetchOptions))
        fetch(BASE_URL+url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                callback(json);
            }).done();
    }

    //get请求
    /**
    *url :请求地址
    *callback:回调函数
    */
    static get(url,params,callback) {
        let baseUrl=BASE_URL+url
        let token=user.user.token?user.user.token:''
        if (params) {
            let paramsArray = [];
            //拼接参数
            Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
            if (baseUrl.search(/\?/) === -1) {
                baseUrl += '?' + paramsArray.join('&')
            } else {
                baseUrl += '&' + paramsArray.join('&')
            }
        }
        var fetchOptions = {
            method: 'GET',
            headers: {
                'Authorization': token
            }
        }
        fetch(baseUrl,fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                callback(json);
            }).done();
    }

    static upload(url,params,callback){
        console.log(params)
        let formData=new FormData()
        for (let key in params){
            formData.append(key, params[key]);
        }
        let file = {uri: params.path, type: 'multipart/form-data', name: 'image.png'};
        formData.append("file", file);
        let baseUrl=BASE_URL+url
        let token=user.user.token?user.user.token:''
        let fetchOptions = {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Authorization': token,
                'Content-Type': 'multipart/form-data;charset=utf-8',
            },
            body: formData
        }
        fetch(baseUrl,fetchOptions).then((response)=>response.json()).then((json)=>{
            callback(json);
        }).done()
    }

}

module.exports = NetUtil;
